package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.db.domain.MarketBrandExample;
import com.cskaoyan.market.db.mapper.MarketBrandMapper;
import com.cskaoyan.market.service.MarkerBrandService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author CMQ
 * @since 2023/07/24 16:10
 */

public class MarketBrandServiceImpl implements MarkerBrandService {
    @Override
    public List<MarketBrand> list(String page, String limit, String id, String name, String sort, String order) {
        SqlSession session = MybatisUtil.getSession();
        MarketBrandMapper marketBrandMapper = session.getMapper(MarketBrandMapper.class);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        // 判空
        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.parseInt(id));
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        marketBrandExample.setOrderByClause(sort + " " + order);// 返回add_time desc

        page = "1";
        limit = "10";
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));

        List<MarketBrand> marketBrandList = marketBrandMapper.selectByExample(marketBrandExample);
        session.commit();
        session.close();
        return marketBrandList;
    }

    @Override
    public MarketBrand create(MarketBrand brand) {
        SqlSession session = MybatisUtil.getSession();
        MarketBrandMapper marketBrandMapper = session.getMapper(MarketBrandMapper.class);

        marketBrandMapper.insertSelective(brand);
        session.commit();
        session.close();
        return brand;
    }

    @Override
    public boolean delete(String id) {
        SqlSession session = MybatisUtil.getSession();
        MarketBrandMapper marketBrandMapper = session.getMapper(MarketBrandMapper.class);
        marketBrandMapper.deleteByPrimaryKey(Integer.parseInt(id));
        session.commit();
        session.close();
        return true;


    }

    @Override
    public MarketBrand update(MarketBrand marketBrand) {
        SqlSession session = MybatisUtil.getSession();
        MarketBrandMapper marketBrandMapper = session.getMapper(MarketBrandMapper.class);

        marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
        session.commit();
        session.close();

        return marketBrand;
    }


}
